package cn.kys.modules.system.controller;

import cn.kys.modules.system.domain.vo.Result;
import cn.kys.modules.system.service.FormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/form_api")
public class FormApi {
    @Autowired
    private FormService formService;

    @GetMapping("/getAllForms")
    public Map getAllForms() {
        Map returnMap = new HashMap();
        try {
            List list = formService.getAllForms();
            returnMap.put("data", list);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //添加表单
    @GetMapping("/addForm")
    public Map addForm(@RequestParam("name") String name) {
        Map returnMap = new HashMap();
        try {
            Map map = formService.addForm(name);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //编辑表单
    @GetMapping("/editForm")
    public Map editForm(@RequestParam("id") String id, @RequestParam("name") String name, @RequestParam("formcontentids") String formcontentids) {
        Map returnMap = new HashMap();
        try {
            Map map = formService.editForm(id, name, formcontentids);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //更新表单内容项的显隐
    @GetMapping("/updateFormContentShowStatus")
    public Map updateFormContentShowStatus(@RequestParam("ids") String ids) {
        Map returnMap = new HashMap();
        try {
            formService.updateFormContentShowStatus(ids);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //获取已经绑定的formcontent
    @GetMapping("/getBindingByFormId")
    public Map getBindingByFormId(@RequestParam("id") String id) {
        Map returnMap = new HashMap();
        try {
            List list = formService.getBindingByFormId(id);
            returnMap.put("data", list);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    @GetMapping("/delFormById")
    public Result delFormById(@RequestParam("id") String id) {
        Map map = formService.delFormById(id);
        return Result.success(map);
    }

}